<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/1/20
 * Time: 10:53
 */

namespace app\common\service;


use app\models\EtcKg;
use yii\base\Exception;

class KaiGuan
{
    const SYS_RUNNING = 'sys_running';
    const MULTI_RULES = 'multi_rules';

    /**
     * @param $key
     * @return bool
     * @throws Exception
     */
    public function isRunningKg($key){
        $row = $this->getKg($key);
        return !!$row->status;
    }

    /**
     * @param $key
     * @return EtcKg
     * @throws Exception
     */
    public function getKg($key){
        $row = EtcKg::find()->andWhere(['kg_str'=>$key])->one();
        if(!$row instanceof EtcKg){
            throw new Exception("{$key}开关不存在");
        }
        return $row;
    }

    /**
     * @param $key
     * @return bool
     * @throws Exception
     */
    public function OpenKG($key){
        $row = $this->getKg($key);
        $row->status = 1;
        return $row->save();
    }

    /**
     * @param $key
     * @return bool
     * @throws Exception
     */
    public function CloseKG($key){
        $row = $this->getKg($key);
        $row->status = 0;
        return $row->save();
    }
}